import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

// Create an OpenAI API client (that's edge friendly!)
// const openai = new OpenAI({
//   apiKey: process.env.OPENAI_API_KEY || '',
// });

const openai = new OpenAI({
  baseURL: "http://10.11.27.60:11434/v1",
  apiKey: "ollama", // required but unused
});


// IMPORTANT! Set the runtime to edge
export const runtime = 'edge';

export async function POST(req: Request) {
  const { messages } = await req.json();

  // Ask OpenAI for a streaming chat completion given the prompt
  // const response = await openai.chat.completions.create({
  //   model: 'gpt-3.5-turbo',
  //   stream: true,
  //   messages,
  // });

  const completion = await openai.chat.completions.create({
    // model: "llama2-chinese:13b-chat-fp16",
    model: "gemma:7b-instruct-q6_K",
    stream: true,
    messages
  });
  // console.log(completion.choices[0].message.content)
  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(completion);
  // Respond with the stream
  return new StreamingTextResponse(stream);
}
